package com.open.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;
import com.open.system.domain.LoanApplyLogDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanApplyLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 借款日志
 */
 
@Controller
@RequestMapping("/system/loanApplyLog")
public class LoanApplyLogController extends BaseController {
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@GetMapping()
	@RequiresPermissions("system:loanApplyLog:loanApplyLog")
	String LoanApplyLog(){
	    return "system/loanApplyLog/loanApplyLog";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanApplyLog:loanApplyLog")
	public PageUtils list(@RequestParam Map<String, Object> params){
		String shopId=getUser().getEmail();//店铺ID
		if(shopId!=null && shopId!="" && !"1".equals(shopId)){
			params.put("shopId",shopId);
		}
		//查询列表数据
        Query query = new Query(params);
        List<LoanApplyLogDO> loanApplyLogList=null;
        int total=0;
        if("1".equals(shopId)){
        	loanApplyLogList= loanApplyLogService.list(query);
        	total = loanApplyLogService.count(query);
        }else {
        	loanApplyLogList = loanApplyLogService.list2(query);
    		total = loanApplyLogService.count2(query);
		}
		PageUtils pageUtils = new PageUtils(loanApplyLogList, total);
		return pageUtils;
	}

    @ResponseBody
    @GetMapping("/getProdDetail")
    @RequiresPermissions("system:loanApplyLog:loanApplyLog")
    public String getProdDetail(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<LoanApplyLogDO> loanApplyLogList = loanApplyLogService.list(query);
        ArrayList<HashMap<String, Object>> objects = CollectionUtil.newArrayList();
        //所申请的借贷产品详情：是否成功借款，借款情况，还款情况（已还、待还、每月需还多少，每月几号是还款日、还款方式、利息多少、总期数）
        for (int i = 0; i < loanApplyLogList.size(); i++) {
            HashMap<String, Object> objectHashMap = MapUtil.newHashMap();
            //产品名称
            objectHashMap.put("productName", loanApplyLogList.get(i).getProductName());
            //每月几号是还款日
            objectHashMap.put("repaymentTime", loanApplyLogList.get(i).getRepaymentTime());
            //已还
            objectHashMap.put("alreadyRepaymentAmt", loanApplyLogList.get(i).getAlreadyRepaymentAmt());
            //待还
            objectHashMap.put("hasRepaymentAmt", loanApplyLogList.get(i).getRemainRepaymentAmt());
            //利息
            objectHashMap.put("interest", loanApplyLogList.get(i).getInterest());
            //总期数
            objectHashMap.put("loanCycle", loanApplyLogList.get(i).getLoanCycle());

            objects.add(objectHashMap);
        }
        return JSONUtil.parseObj(objects).toString();
    }

	@GetMapping("/add")
	@RequiresPermissions("system:loanApplyLog:add")
	String add(){
	    return "system/loanApplyLog/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanApplyLog:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanApplyLogDO loanApplyLog = loanApplyLogService.get(id);
		model.addAttribute("loanApplyLog", loanApplyLog);
	    return "system/loanApplyLog/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanApplyLog:add")
	public R save( LoanApplyLogDO loanApplyLog){
		if(loanApplyLogService.save(loanApplyLog)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanApplyLog:edit")
	public R update( LoanApplyLogDO loanApplyLog){
		loanApplyLogService.update(loanApplyLog);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanApplyLog:remove")
	public R remove( Long id){
		if(loanApplyLogService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanApplyLog:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanApplyLogService.batchRemove(ids);
		return R.ok();
	}
	
}
